SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ApplicationInformation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ApplicationInformation](
	[Id] [uniqueidentifier] NOT NULL,
	-- TODO - put other application-specific info in here such as license key???
	[Version] [varchar](25) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[UserSessionId] [uniqueidentifier] not null,
	[WorkingSet] bigint not null
 CONSTRAINT [PK_ApplicationInformation] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ExceptionInformation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ExceptionInformation](
	[Id] [uniqueidentifier] NOT NULL,
	[CallStack] [varchar](2500) NOT NULL,
	[ExceptionName] [varchar](100) NOT NULL,
	[Message] [varchar](200) NOT NULL,
	[EmailAddress] [varchar](200) NULL,
	[ErrorDescription] [varchar](1000) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[SystemInfoId] [uniqueidentifier] NOT NULL
 CONSTRAINT [PK_ExceptionInformation] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SystemInformation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SystemInformation](
	[Id] [uniqueidentifier] NOT NULL,
	[OperatingSystem] [varchar](255) NULL,
	[IsTabletPc] [bit] NOT NULL,
	[ScreenWidth] [int] NOT NULL,
	[ScreenHeight] [int] NOT NULL,
	[RenderCapabilityTier] [int] NOT NULL,
	[IsSlowMachine] [bit] NOT NULL,
	[ProcessorCount] [int] NOT NULL,
	[ProcessorArchitecture] [varchar](20) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[UserNameHash] [int] not null,
	[MachineNameHash] [int] not null,
	[DomainHash] [int] not null,
	[CLRVersion] varchar(40) not null,
	[PhysicalMemory] [bigint] not null
 CONSTRAINT [PK_SystemInformation] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FeatureUsageRecord]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[FeatureUsageRecord](
	[Id] [uniqueidentifier] NOT NULL,
	[SystemInfoId] [uniqueidentifier] NOT NULL,
	[ApplicationInfoId] [uniqueidentifier] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_FeatureUsageRecord] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FeatureUsageInformation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[FeatureUsageInformation](
	[Id] [uniqueidentifier] NOT NULL,
	[FeatureUsageRecordId] [uniqueidentifier] NOT NULL,
	[FeatureName] [varchar](255) NOT NULL,
	[UsageCount] [int] NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_FeatureUsageInfo] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_FeatureUsageRecord_ApplicationInformation]') AND parent_object_id = OBJECT_ID(N'[dbo].[FeatureUsageRecord]'))
ALTER TABLE [dbo].[FeatureUsageRecord]  WITH CHECK ADD  CONSTRAINT [FK_FeatureUsageRecord_ApplicationInformation] FOREIGN KEY([ApplicationInfoId])
REFERENCES [dbo].[ApplicationInformation] ([Id])
GO
ALTER TABLE [dbo].[FeatureUsageRecord] CHECK CONSTRAINT [FK_FeatureUsageRecord_ApplicationInformation]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_FeatureUsageRecord_SystemInformation]') AND parent_object_id = OBJECT_ID(N'[dbo].[FeatureUsageRecord]'))
ALTER TABLE [dbo].[FeatureUsageRecord]  WITH CHECK ADD  CONSTRAINT [FK_FeatureUsageRecord_SystemInformation] FOREIGN KEY([SystemInfoId])
REFERENCES [dbo].[SystemInformation] ([Id])
GO
ALTER TABLE [dbo].[FeatureUsageRecord] CHECK CONSTRAINT [FK_FeatureUsageRecord_SystemInformation]
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ExceptionInformation_SystemInformation]') AND parent_object_id = OBJECT_ID(N'[dbo].[ExceptionInformation]'))
ALTER TABLE [dbo].[ExceptionInformation]  WITH CHECK ADD  CONSTRAINT [FK_ExceptionInformation_SystemInformation] FOREIGN KEY([Id])
REFERENCES [dbo].[SystemInformation] ([Id])
GO
ALTER TABLE [dbo].[ExceptionInformation] CHECK CONSTRAINT [FK_ExceptionInformation_SystemInformation]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_FeatureUsageInformation_FeatureUsageRecord]') AND parent_object_id = OBJECT_ID(N'[dbo].[FeatureUsageInformation]'))
ALTER TABLE [dbo].[FeatureUsageInformation]  WITH CHECK ADD  CONSTRAINT [FK_FeatureUsageInformation_FeatureUsageRecord] FOREIGN KEY([FeatureUsageRecordId])
REFERENCES [dbo].[FeatureUsageRecord] ([Id])
GO
ALTER TABLE [dbo].[FeatureUsageInformation] CHECK CONSTRAINT [FK_FeatureUsageInformation_FeatureUsageRecord]
